package com.sync.utils.project.gasStatistics.agent.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sync.utils.common.enums.ResultCodeEnum;
import com.sync.utils.common.enums.URLEnum;
import com.sync.utils.common.exception.BusinessException;
import com.sync.utils.framework.okHttp.OkHttpUtils;
import com.sync.utils.project.gasStatistics.agent.GasAgent;
import com.sync.utils.project.gasStatistics.pojo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2025/3/16 9:28
 */
@Service
public class GasAgentImpl implements GasAgent {

    protected final static Logger logger = LoggerFactory.getLogger(GasAgentImpl.class);

    /**
     * 获取网络端天然气使用月账单数据
     *
     * @param gasReq
     * @return
     */
    @Override
    public List<GasMonthRes> getHttpGasMonthBillList(GasMonthReq gasReq) {
        String rs = OkHttpUtils.builder().url(URLEnum.GAS_MONTH_TOTAL.getUrl())
                // 有参数的话添加参数，可多个
                .addBodyRaw(gasReq)
                // 也可以添加多个
                .post(true)
                .sync();
        logger.info("网络查询结果：{}", JSON.parseObject(rs, GasResult.class));
        GasResult<GasMonthData> gasResult = JSON.parseObject(rs, new TypeReference<GasResult<GasMonthData>>(){});
        if (ObjectUtil.isEmpty(gasResult)) {
            throw new BusinessException(ResultCodeEnum.RESULT_NULL);
        }
        if (!gasResult.getSuccess()) {
            throw new BusinessException(gasResult.getCode(), gasResult.getMessage());
        }
        List<GasMonthRes> recordsInfo = gasResult.getData().getRecordsInfo();
        return recordsInfo;
    }

    @Override
    public List<GasDayRes> getHttpGasDayBillList(String mdmCode, String userCode) {
        String rs = OkHttpUtils.builder().url(URLEnum.GAS_DAY_TOTAL.getUrl() +"/"+ mdmCode + "/" + userCode)
                // 也可以添加多个
                .post(false)
                .sync();
        logger.info("网络查询结果：{}", JSON.parseObject(rs, GasResult.class));
        GasResult<GasDayData> gasResult = JSON.parseObject(rs, new TypeReference<GasResult<GasDayData>>(){});
        if (ObjectUtil.isEmpty(gasResult)) {
            throw new BusinessException(ResultCodeEnum.RESULT_NULL);
        }
        if (!gasResult.getSuccess()) {
            throw new BusinessException(gasResult.getCode(), gasResult.getMessage());
        }
        List<GasDayRes> recordsInfo = gasResult.getData().getRecordsInfo();
        return recordsInfo;
    }

    @Override
    public GasUserRes getHttpGasUserInfo(String userCode) {
        GasUserReq gasUserReq = new GasUserReq(userCode, "5000000961");
        String rs = OkHttpUtils.builder().url(URLEnum.GAS_USER_INFO.getUrl())
                .addBodyRaw(gasUserReq)
                .post(true)
                .sync();
        logger.info("网络查询结果：{}", JSON.parseObject(rs, GasResult.class));
        GasResult<GasUserRes> gasUserInfo = JSON.parseObject(rs, new TypeReference<GasResult<GasUserRes>>(){});
        if (ObjectUtil.isEmpty(gasUserInfo)) {
            throw new BusinessException(ResultCodeEnum.RESULT_NULL);
        }
        if (!gasUserInfo.getSuccess()) {
            throw new BusinessException(gasUserInfo.getCode(), gasUserInfo.getMessage());
        }
        return gasUserInfo.getData();
    }
}
